#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_migliore\utility;


init()
{
    level thread onPlayerConnected();
    
    InitClassesData();
}

onPlayerConnected()
{
    for(;;) {
        level waittill("connected", player);
        
        player thread onPlayerJoinedSpectators();
    }
}

onPlayerJoinedSpectators()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("joined_spectators");
        
        // разблокировка класса при выходе в зрители
        if(self.zClass_Current != -1) {
            level.zClass_List[self.zClass_Current].busy = undefined;
            self.zClass_Current = -1;
        }
    }
}

InitClassesData()
{
    level.zClass_List = [];
    
    level.zClass_List[0] = spawnStruct();
    level.zClass_List[0].name = "AA Specialist"; // название класса
    level.zClass_List[0].defaultPrimary = "mp5k_mp"; // стандартное оружие
    level.zClass_List[0].defaultSecondary = "at4_mp"; // дополнительное оружие
    level.zClass_List[0].speed = 0.95; // множитель скорости игрока
    level.zClass_List[0].skin = "smg"; // скин игрока
    level.zClass_List[0].icon = "cardicon_launcher"; // статус-иконка класса в статистике по Tab
    level.zClass_List[0].skills = []; // массив с функциями-возможностями
    level.zClass_List[0].skills[0] = ::aa_common; // вызываются как thread, endon: death, disconnect, zclass_changed
    level.zClass_List[0].busy = undefined; // кем занят класс
    
    level.zClass_List[1] = spawnStruct();
    level.zClass_List[1].name = "Assault";
    level.zClass_List[1].defaultPrimary = "m4_gl_mp";
    level.zClass_List[1].defaultSecondary = "beretta_mp";
    level.zClass_List[1].speed = 0.95;
    level.zClass_List[1].skin = "assault";
    level.zClass_List[1].icon = "cardicon_loadedfinger";
    level.zClass_List[1].skills = [];
    level.zClass_List[1].skills[0] = ::assault_common;
    level.zClass_List[1].busy = undefined;
    
    level.zClass_List[2] = spawnStruct();
    level.zClass_List[2].name = "Engineer";
    level.zClass_List[2].defaultPrimary = "m4_shotgun_mp";
    level.zClass_List[2].defaultSecondary = "beretta_mp";
    level.zClass_List[2].speed = 1.0;
    level.zClass_List[2].skin = "shotgun";
    level.zClass_List[2].icon = "cardicon_shotgun_shells";
    level.zClass_List[2].skills = [];
    level.zClass_List[2].skills[0] = ::engineer_common;
    level.zClass_List[2].busy = undefined;
    
    level.zClass_List[3] = spawnStruct();
    level.zClass_List[3].name = "Heavy";
    level.zClass_List[3].defaultPrimary = "sa80_grip_xmags_mp";
    level.zClass_List[3].defaultSecondary = "beretta_mp";
    level.zClass_List[3].speed = 0.9;
    level.zClass_List[3].skin = "lmg";
    level.zClass_List[3].icon = "cardicon_minigun";
    level.zClass_List[3].skills = [];
    level.zClass_List[3].skills[0] = ::heavy_common;
    level.zClass_List[3].busy = undefined;
    
    level.zClass_List[4] = spawnStruct();
    level.zClass_List[4].name = "Medic";
    level.zClass_List[4].defaultPrimary = "m4_mp";
    level.zClass_List[4].defaultSecondary = "beretta_mp";
    level.zClass_List[4].speed = 0.95;
    level.zClass_List[4].skin = "camo";
    level.zClass_List[4].icon = "cardicon_pacifier_pink";
    level.zClass_List[4].skills = [];
    level.zClass_List[4].skills[0] = ::medic_common;
    level.zClass_List[4].skills[1] = ::medic_healing_over_time;
    level.zClass_List[4].busy = undefined;
    
    level.zClass_List[5] = spawnStruct();
    level.zClass_List[5].name = "Recon";
    level.zClass_List[5].defaultPrimary = "cheytac_fmj_mp";
    level.zClass_List[5].defaultSecondary = "usp_silencer_mp";
    level.zClass_List[5].speed = 0.90;
    level.zClass_List[5].skin = "ghillie";
    level.zClass_List[5].icon = "cardicon_ghillie";
    level.zClass_List[5].skills = [];
    level.zClass_List[5].skills[0] = ::recon_common;
    level.zClass_List[5].busy = undefined;
    
    
    // precache class status-icons
    for(i = 0; i < level.zClass_List.size; i++)
        precacheStatusIcon(level.zClass_List[i].icon);
}

empty_func()
{
    self endon("death");
    self endon("disconnect");
    self endon("zclass_changed");
    
    wait(3.0);
    self iPrintLn("Nothing happend!");
}

aa_common()
{
    self.isSniper = false;
    
    self setOffhandPrimaryClass("frag");
    self maps\mp\perks\_perks::givePerk("frag_grenade_mp");
    self giveMaxAmmo("frag_grenade_mp");
    
    self setOffhandSecondaryClass("smoke");
    self giveWeapon("concussion_grenade_mp", 0, false);
    self giveMaxAmmo("concussion_grenade_mp");
}

assault_common()
{
    self.isSniper = false;
    
    self setOffhandPrimaryClass("other");
    self maps\mp\perks\_perks::givePerk("semtex_mp");
    self giveMaxAmmo("semtex_mp");
    
    self maps\mp\perks\_perks::givePerk("specialty_fastreload");
    
    self setOffhandSecondaryClass("flash");
    self giveWeapon("flash_grenade_mp", 0, false);
    self giveMaxAmmo("flash_grenade_mp");
}

engineer_common()
{
    self.isSniper = false;
    
    self setOffhandPrimaryClass("other");
    self maps\mp\perks\_perks::givePerk("c4_mp");
    self giveMaxAmmo("c4_mp");
}

heavy_common()
{
    self.isSniper = false;
    
    self setOffhandPrimaryClass("frag");
    self maps\mp\perks\_perks::givePerk("frag_grenade_mp");
    self giveMaxAmmo("frag_grenade_mp");
}

medic_common()
{
    self.isSniper = false;
    
    self setOffhandPrimaryClass("throwingknife");
    self maps\mp\perks\_perks::givePerk("throwingknife_mp");
    self giveMaxAmmo("throwingknife_mp");
    
    self maps\mp\perks\_perks::givePerk("specialty_pistoldeath");
}

medic_healing_over_time()
{
    self endon("death");
    self endon("disconnect");
    self endon("zclass_changed");
    
    for(;;) {
        wait(1.0);
        
        if(self.health < self.maxhealth)
            self.health++;
    }
}

recon_common()
{
    self.isSniper = true;
    
    self setOffhandPrimaryClass("other");
    self maps\mp\perks\_perks::givePerk("c4_mp");
    self giveMaxAmmo("c4_mp");
    
    self setOffhandSecondaryClass("smoke");
    self giveWeapon("smoke_grenade_mp", 0, false);
    self giveMaxAmmo("smoke_grenade_mp");
}

changeClass(new_class)
{
    self notify("zclass_changed");
    
    self.statusicon = level.zClass_List[new_class].icon;
    
    self takeAllWeapons();
    
    self.zClass_PrimaryWeapon = level.zClass_List[new_class].defaultPrimary;
    self.zClass_SecondaryWeapon = level.zClass_List[new_class].defaultSecondary;
    
    self z_GiveWeapons();
    
    self _clearPerks();
    self maps\mp\gametypes\_class::_detachAll();
    
    self.pers["primaryWeapon"] = self.zClass_PrimaryWeapon;
    self.primaryWeapon = self.zClass_PrimaryWeapon;
    self.secondaryWeapon = self.zClass_SecondaryWeapon;
    
    self thread maps\mp\gametypes\_weapons::stowedWeaponsRefresh();
    
    self maps\mp\gametypes\_teams::playerCustomModel(level.zClass_List[new_class].skin);
    self maps\mp\gametypes\_weapons::customUpdateMoveSpeedScale(level.zClass_List[new_class].speed);
    
    for(i = 0; i < level.zClass_List[new_class].skills.size; i++) {
        self thread [[ level.zClass_List[new_class].skills[i] ]]();
    }
    
    if(self.zClass_Current != -1)
        level.zClass_List[self.zClass_Current].busy = undefined;
    
    self.zClass_Current = new_class;
    level.zClass_List[self.zClass_Current].busy = self;
    
    self iPrintLnBold("You've switched to ^1" + level.zClass_List[self.zClass_Current].name);
}

giveClassLoadout()
{
    self.statusicon = level.zClass_List[self.zClass_Current].icon;
    
    self takeAllWeapons();
    
    // initialize specialty array
    self.specialty = [];
    
    self.class_num = 0;
    self.curClass = "custom1";
    
    self.loadoutPrimaryCamo = 0;
    self.loadoutPrimary = "none";
    self.loadoutSecondary = "beretta";
    self.loadoutSecondaryCamo = 0;
    
    self z_GiveWeapons();
    
    self SetOffhandPrimaryClass("other");
    self SetOffhandSecondaryClass("flash");
    
    self _SetActionSlot(1, "");
    //self _SetActionSlot(2, "nightvision");
    self _SetActionSlot(3, "altMode");
    self _SetActionSlot(4, "");

    self _clearPerks();
    self maps\mp\gametypes\_class::_detachAll();
    
    // перки
    //self maps\mp\perks\_perks::givePerk("specialty_pistoldeath");
    
    self maps\mp\gametypes\_class::setKillstreaks("none", "none", "none");
    
    self.pers["primaryWeapon"] = self.zClass_PrimaryWeapon;
    self.primaryWeapon = self.zClass_PrimaryWeapon;
    self.secondaryWeapon = self.zClass_SecondaryWeapon;
    
    self thread maps\mp\gametypes\_weapons::stowedWeaponsRefresh();
    
    self maps\mp\gametypes\_teams::playerCustomModel(level.zClass_List[self.zClass_Current].skin);
    self maps\mp\gametypes\_weapons::customUpdateMoveSpeedScale(level.zClass_List[self.zClass_Current].speed);
    
    self.isSniper = false;
    
    // cac specialties that require loop threads
    self maps\mp\perks\_perks::cac_selector();
    
    for(i = 0; i < level.zClass_List[self.zClass_Current].skills.size; i++) {
        self thread [[ level.zClass_List[self.zClass_Current].skills[i] ]]();
    }
    
    self notify("changed_kit");
    self notify("giveLoadout");
}

